﻿using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using HML.Design;

namespace HML
{
    /// <summary>
    /// 重写TabControlPlus只允许添加TabPagePlus类型的子控件
    /// </summary>
    [ComVisible(false)]
    [Editor(typeof(TabControlPlusControlsCollectionEditor), typeof(UITypeEditor))]
    public class TabControlPlusControlsCollection : Control.ControlCollection
    {
        #region 字段

        private TabControlPlus owner;

        #endregion

        public TabControlPlusControlsCollection(TabControlPlus owner) : base(owner)
        {
            this.owner = owner;
        }

        #region 重写

        public override void Add(Control value)
        {
            if (!(value is TabPagePlus))
            {
                throw new ArgumentException("添加的类型必须为 TabPagePlus");
            }

            TabPagePlus tabPage = (TabPagePlus)value;
            tabPage.Visible = false;
            base.Add(tabPage);
            owner.AddTabPage(tabPage);
        }

        public override void Remove(Control value)
        {
            base.Remove(value);
            if (!(value is TabPagePlus))
            {
                return;
            }

            int index = owner.FindTabPage((TabPagePlus)value);
            if (index != -1)
            {
                owner.RemoveTabPage(index);
            }

        }

        #endregion

    }

}
